package com.gobo.cmsservice.controller;

import com.gobo.cmsservice.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gobo.cmsservice.pojo.CrmBanner;
import com.gobo.cmsservice.pojo.vo.QueryBanner;
import com.gobo.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-22
 */
@RestController
@RequestMapping("/cmsservice/banner")
public class CrmBannerController {

    @Autowired
    private CrmBannerService bannerService;

    /**
     * 增加首页轮播图
     * @param crmBanner
     * @return
     */
    @PostMapping("save")
    public R saveBanner(@RequestBody CrmBanner crmBanner){
        Boolean result = bannerService.saveBanner(crmBanner);
        return result?R.ok():R.error();
    }

    /**
     * 更新轮播图
     * @param crmBanner
     * @return
     */
    @PostMapping("/update")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        Boolean result = bannerService.updateBanner(crmBanner);
        return result?R.ok():R.error();
    }

    /**
     * 删除轮播图
     * @param bannerId
     * @return
     */
    @DeleteMapping("remove/{bannerId}")
    public R removeBanner(@PathVariable String bannerId){
        Boolean result = bannerService.removeBanner(bannerId);
        return result?R.ok():R.error();
    }

    /**
     * 获取所有的轮播图
     * @return
     */
    @GetMapping("getAll")
    public R getBanner(){
        List<CrmBanner> bannerList = bannerService.getAllBanner();
        return R.ok().data("bannerList",bannerList);
    }
    @GetMapping("getBannerById/{bannerId}")
    public R getBannerById(@PathVariable String bannerId){
        CrmBanner banner = bannerService.getById(bannerId);
        return R.ok().data("item",banner);
    }

    /**
     * 获取分页轮播图
     * @param current
     * @param size
     * @return
     */
    @PostMapping("getPage/{current}/{size}")
    public R getBanner(@PathVariable Long current, @PathVariable Long size, @RequestBody QueryBanner queryBanner){
        Page<CrmBanner> page = new Page<>(current,size);
        bannerService.getPageBanner(page,queryBanner);
        return R.ok().data("items",page.getRecords()).data("total",page.getTotal());
    }


}

